"""Dummy robot only."""

import logging
import random

from waveapi import events
from waveapi import model
from waveapi import robot


def on_self_added(properties, context):
  """Invoked when any participants have been added/removed."""
  added = properties['participantsAdded']
  for p in added:
    if p == 'kay-the-bot@appspot.com':
      Setup(context)
      break

class converter(object):
  def __init__(self, chars):
    self.original_chars = chars
    self.chars = chars.lower() + chars.upper()
  def convert(self, letter):
    if letter in self.chars:
      return self.original_chars[random.randint(0, len(self.original_chars)-1)]
    return None

converters = []
converters.append(converter("bdgtl"))
converters.append(converter("fh"))
converters.append(converter("mn"))
converters.append(converter("pq"))

def _convert(s):
  for c in converters:
    ret = c.convert(s)
    if ret:
      return ret
  return s

def on_submitted(properties, context):
  """
  """
  blip = context.GetBlipById(properties['blipId'])
  words = blip.GetDocument().GetText()
  logging.debug("type(words): %s" % type(words))
  logging.debug("words: %s" % words)
  if words.strip():
    blip.CreateChild().GetDocument().SetText("".join([_convert(l) for l in words]))


def Setup(context):
  """Called when this robot is first added to the wave."""
  root_wavelet = context.GetRootWavelet()
  root_wavelet.CreateBlip().GetDocument().SetText("Baa")


if __name__ == '__main__':
  logging.getLogger().setLevel(logging.DEBUG)
  dummy = robot.Robot('Kay', '1.0',
                      image_url='http://kay-the-bot.appspot.com/icon.png',
                      profile_url='http://kay-the-bot.appspot.com/_wave/robot/profile')
  dummy.RegisterHandler(events.WAVELET_SELF_ADDED,
                        on_self_added)
  dummy.RegisterHandler(events.BLIP_SUBMITTED,
                        on_submitted)
  dummy.Run(debug=True)
